﻿using ReflectionStudio.Core.Events;
using System;
using System.Data;
using System.ComponentModel;
namespace WPFExcelReport
{
    public partial class TKD05 : UIKDADR
    {

        public TKD05()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VKUNDADR();
            DataContext = MV;
            InitializeComponent();
        }
        public override void NewCommand(object sender, System.Windows.RoutedEventArgs e)
        {
            try
            {
                dwData = TBMain.NewRow();
                dwData["KDAMANDANT"] = app.Mandant;
                dwData["KDAKUNDNR"] = "AND KDAKUNDNR='"+ KUNDNR+"'";
                dwData["KDAADRSL"] = "AND KDAADRSL='" + "VK".PadRight(8) + "'";
                SResult = TMV.QueryKUNDADR(dwData, 1);
                foreach (DataRow row in SResult.Tables[0].Rows)
                {
                    if (KDAADRSL == "") KDAADRSL = "TH".PadRight(8);
                    row["KDAADRSL"] = KDAADRSL;
                    if (TBMain.Rows.Count > 0)
                    {
                        TMV.UpdateKUNDADR(row, app.company, 0);
                    }
                    else
                    {
                        TMV.InsertKUNDADR(row, app.company);
                    }
                    ORDERNUM = "";
                    ORDERNUM = KUNDNR + KDAADRSL;//会执行载入
                    break;
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        public override void Window_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {

            base.Window_Loaded(sender, e);

        }

        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
            if (dwMaster == null)
            { 
                KDANAME1 = ""; 
                KDANAME2 = "";
                KDASTRASSE1 = "";
                KDASTRASSE2 = "";
                KDAPOSTFACH = "";
                KDATEL = "";
                KDATTEX = "";
                KDAFAX = "";
                KDAMAILADR = "";
            }
            else
            {  
                KDANAME1 = dwMaster["KDANAME1"].ToString(); 
                KDANAME2 = dwMaster["KDANAME2"].ToString();
                KDASTRASSE1 = dwMaster["KDASTRASSE1"].ToString();
                KDASTRASSE2 = dwMaster["KDASTRASSE2"].ToString();
                KDAPOSTFACH = dwMaster["KDAPOSTFACH"].ToString();
                KDATEL = dwMaster["KDATEL"].ToString();
                KDATTEX = dwMaster["KDATTEX"].ToString();
                KDAFAX = dwMaster["KDAFAX"].ToString();
                KDAMAILADR = dwMaster["KDAMAILADR"].ToString();
            }
        }

        public override void OnSave()
        {
            try
            {
                if (!IsSaved)
                    return;
                if (dwMaster == null)
                    return; 
                TMV.UpdateKUNDADR(dwMaster, app.company, 0);
                string _key = dwMaster["KDAKUNDNR"].ToString() + dwMaster["KDAADRSL"].ToString();
                KEY = "";
                KEY = _key;//触发关联界面更改
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        VKDSTAMM vkd;

        public override void LOADBYAUF()
        {
            try
            {
                base.LOADBYAUF();
                if (ORDERNUM.Length < 16)
                    return;
                Locate("KEY", ORDERNUM);
                if (dwMaster != null)
                    return;
              
                  /*  string _ADRSL = ORDERNUM.Substring(8, 8);
              if (_ADRSL.Trim() != KDAADRSL.Trim())
                    return;*/
                //KUNDNR = ORDERNUM.Substring(0, 8);
              //  string _KUNDER = KUNDNR;
                dwData = TBMain.NewRow();
                dwData["KDAMANDANT"] = app.Mandant;
                dwData["KDAKUNDNR"] = " AND KDAKUNDNR='" + ORDERNUM.Substring(0, 8) + "'";
                dwData["KDAADRSL"] = " AND  KDAADRSL='" + ORDERNUM.Substring(8, 8) + "'";
                SResult = TMV.QueryKUNDADR(dwData, 1);
                MergeData(SResult, true, true);
                if (!IsAuto)
                    return;
                if (TBMain.Rows.Count > 0)
                    return;

                if (vkd == null) vkd = new VKDSTAMM();
                dwData = vkd.TBMain.NewRow();
                dwData["KDMANDANT"] = app.Mandant;
                dwData["KDKUNDNR"] = KUNDNR;
                SResult = vkd.QueryKDSTAMM(dwData, 1);
                foreach (DataRow kd in SResult.Tables[0].Rows)
                {
                    dwData = TBMain.NewRow();
                    TMV.SetUpNewRow(ref dwData);
                    dwData["KDAMANDANT"] = app.Mandant;
                    dwData["KDAKUNDNR"] = KUNDNR;
                    dwData["KDAADRSL"] = KDAADRSL;
                    dwData["KDANAME1"] = kd["KDNAME1"];
                    dwData["KDANAME2"] = kd["KDNAME2"];
                    dwData["KDASTRASSE1"] = kd["KDSTRASSE1"];
                    dwData["KDASTRASSE2"] = kd["KDSTRASSE2"];
                    dwData["KDAPOSTFACH"] = kd["KDKNAME"];
                    dwData["KDATEL"] = kd["KDTEL"];
                    dwData["KDATTEX"] = kd["KDTTEX"];
                    dwData["KDAFAX"] = kd["KDFAX"];
                    dwData["KDAPOSTFACH"] = kd["KDPOSTFACH"];
                    dwData["KDAMAILADR"] = kd["KDMAILADR"];
                    TMV.Insert(dwData);
                    vwMaster.View.MoveCurrentToFirst();
                    string key = KUNDNR;
                    KUNDNR = "";
                    KUNDNR = key;//触发列表界面更新 
                    break;
                }
                
                //Locate("KEY", ORDERNUM);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
    }
}